How to Connect to Redis Database via Node.js
In order to store or access the data inside a Redis database, you first need to connect to the Redis database server. We will show you the sample codes to connect your Redis via Node.js.
Connecting via Node.js
Please use the command to install ioredis first: npm install ioredis
Then. get the required information: host, port, username, and password in Cloud Clusters control panel and input the info in the following code.
Next, run the following code.
const Redis = require('ioredis');
const fs = require('fs');
const redis = new Redis({
host: 'redis-xxx-0.cloudclusters.net', // change it to your database server
port: xxx, // change it to your database port
username: 'username',
password: 'user password',
tls: {
rejectUnauthorized: false,
}
});
redis.set('foo', 'bar', (err, reply) => {
if (err) throw err;
console.log(reply);
redis.get('foo', (err, reply) => {
if (err) throw err;
console.log(reply);
});
});